using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy3DCoreSmall : Enemy3DCore
{
    private SeqAction shellAction=new(){beginWaitTime=134f/60f,intervalTime=8f/60f};
    private Sprite[] shellSprites;
    private Transform shellObj;
    private Transform flaskBackObj;
 
    private Sprite residualSprite;

    private bool needDestroy=false;
    void Awake()
    {
        base.Init();
        shellObj=transform.Find("壳1");
        flaskBackObj=transform.Find("FlashBack-蓝");

        shellSprites=new Sprite[]{
            ResourcesPool.Sprites["魂-第二关-壳1"],
            ResourcesPool.Sprites["魂-第二关-壳2"],
            ResourcesPool.Sprites["魂-第二关-壳3"],
        };
        residualSprite=ResourcesPool.Sprites["魂-第二关-残"];

        shellObj.gameObject.SetActive(true);
        flaskBackObj.gameObject.SetActive(false);
        GetComponent<SpriteRenderer>().enabled=false;

    }
    void Start()
    {
        Stop();

    }
    private void FixedUpdate() {
        base.CommonFixedUpdate();
    }

    protected void UpdateStateNone(bool stateChanged, EnumState inState, out EnumState outState)
    {
        outState=EnumState.Working;
    }
    protected void UpdateStateWorking(bool stateChanged, EnumState inState, out EnumState outState){
        outState=inState;
        if(!shellAction.done){
            shellAction.Update((SeqAction seq)=>{
                // int index=seq.seqNo%shellSprites.Length;
                if(seq.seqNo>=shellSprites.Length){
                    seq.done=true;
                    shellObj.gameObject.SetActive(false);
                }else{
                    if(shellObj.TryGetComponent(out SpriteRenderer sr)){
                        sr.sprite=shellSprites[seq.seqNo];
                    }
                }
            });
        }
    }
    protected override void UpdateStateDie(bool stateChanged, EnumState inState, out EnumState outState){
        base.UpdateStateDie(stateChanged,inState,out outState);
        if(stateChanged){
            CreateBoom(transform.position);
            Constant.Sounds.EnemyDeath2.PlayOneShot();
            if(TryGetComponent(out SpriteRenderer sr)){
                sr.sprite=residualSprite;
            }
        }
        if(needDestroy){
            Destroy(gameObject);
        }
    }

    public override float Damage(GameObject source, float value)
    {
        if(!running || !shellAction.done){
            return 0;
        }else{
            return base.Damage(source, value);
        }
    }

    public override void Run()
    {
        if(!running){
            shellObj.gameObject.SetActive(true);
            flaskBackObj.gameObject.SetActive(true);
            GetComponent<SpriteRenderer>().enabled=true;
            running = true;
        }
    }

    public override void Stop()
    {
        if (running)
        {
            shellObj.gameObject.SetActive(true);
            flaskBackObj.gameObject.SetActive(false);
            GetComponent<SpriteRenderer>().enabled=false;
            running = false;
        }
    }

    public override void DieAndDestroy()
    {
        needDestroy=true;
        Die();
    }

}
